<?php


namespace app\opengzh\middleware;



use thans\jwt\facade\JWTAuth;

class JWTauthOpengzh {

    //无需登录名单
    private $n = [
        'OpenPlatform'                =>true,
    ];

    public function handle($request, \Closure $next) {
        //这个中间层校验有没有登录
        //如果没有登录就肯定时1001
        $controller = $request->controller();
        $action = $request->action();

        //不需要登录的控制器，直接过
        if(isset($this->n[$controller])){
            if($this->n[$controller] === true){
                return $next($request);
            }
            if(in_array($action,$this->n[$controller])){
                return $next($request);
            }
        }

        //$action = $request->action();
        if($controller == 'Login'){
            return $next($request);
        }else{
            try{
                $auth_info = JWTAuth::auth();
            }catch (\Exception $e){
                return json(['code'=>1001,'msg'=>$e->getMessage(),'data'=>[]]);
            }
        }

        return $next($request);
    }
}